﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyApp.Infrastructure.Messages;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Composite.Regions;

namespace MyApp
{
    public partial class Shell : UserControl
    {
        public Shell()
        {
            InitializeComponent();
            Messenger.Get<CreateViewMessage>().Subscribe(this.HandleCreateView, ThreadOption.UIThread, true, this.CanHandleCreateView);
        }

        private bool CanHandleCreateView(CreateViewMessageArgs args)
        {
            //these are handled somewhere else
            return args.Position != ViewPosition.Floating && args.Position != ViewPosition.Center;
        }

        private void HandleCreateView(CreateViewMessageArgs args)
        {
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            switch (args.Position)
            {
                case ViewPosition.Bottom: regionManager.Regions["Bottom"].Add(args.View.Value); break;
                case ViewPosition.Top: regionManager.Regions["Top"].Add(args.View.Value); break;
                case ViewPosition.Left: regionManager.Regions["Left"].Add(args.View.Value); break;
                case ViewPosition.Right: regionManager.Regions["Right"].Add(args.View.Value); break;
            }
        }
    }
}
